package hsl.study.controller;


import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;

/**
 * @Description:
 * @Author: hsl
 * @CreateDate: 2025/7/31 17:51
 * @Version: 1.0
 */


@RestController
@RequestMapping("/chatMemory")
public class ChatMemoryController {

    @Resource(name = "deepseekChatClient")
    private ChatClient deepseekChatClient;

    /**
     * @Description: 等价ChatMemory chatMemory = MessageWindowChatMemory.builder().maxMessages(10).build();
     */
    @Resource
    ChatMemory chatMemory;

    /**
     * //http://localhost:7007/chatMemory/chat1?question=1加7等于多少直接给答案&sessionId=2
     *
     * @param question
     * @param sessionId
     * @return
     */
    @GetMapping("/chat1")
    public Flux<String> chat1(String question,String sessionId){

        MessageChatMemoryAdvisor messageChatMemoryAdvisor = MessageChatMemoryAdvisor.builder(chatMemory) // 初始化构建器，需要传入一个ChatMemory实例
                .conversationId(sessionId) // 设置会话ID[可选]
                .scheduler(Schedulers.boundedElastic()) //设置调度器[用于异步写入内存，进行持久化操作]
                .build();

        return deepseekChatClient.prompt()
                .user(question)
                .advisors(messageChatMemoryAdvisor)
                .stream()
                .content();
    }
}
